﻿
// 绑定事件
function BindEvent(element, eventName, fun) {
    if (typeof element === "string") {
        element = document.getElementById(element);
    }

    if (document.attachEvent) {
        element.attachEvent("on" + eventName, fun);
    }
    else {
        element.addEventListener(eventName, fun, false);
    }
}

// 获得事件源对象
function GetEventSrcElement(e) {
    if (window.event) {
        return window.event.srcElement;
    }
    else {
        return e.target;
    }
}

// 根据元素id获得元素
function Get(id) {
    return document.getElementById(id);
}

// 获得元素内部文本
function GetText(element) {
    if (element.innerText !== "undefined") {
        // innerText ie
        return element.innerText;
    }
    else {
        // textContent ff
        return element.textContent;
    }
}

// 设置元素内部文本
function SetText(element, text) {
    if (element.innerText !== "undefined") {
        // innerText ie
        element.innerText = text;
    }
    else {// textContent ff
        element.textContent = text;
    }
}

// 取消事件
function CancelEvent(e) {
    if (window.event) {
        // IE中取消事件
        window.event.returnValue = false;
    }
    else {
        // w3c ff 取消事件
        e.preventDefault();
    }
}

// 取消冒泡
function CancelBubble(e) {
    if (typeof event !== 'undefined') {// event IE
        //if (window.event) {// event IE
        event.cancelBubble = true;
    }
    else { // e w3c
        //alert(e.stopPropagation);
        e.stopPropagation();
    }
}

// 得到输入
function GetCode(e) {
    var code = null;
    if (window.event) {
        code = window.event.keyCode;
    }
    else {
        code = e.which;
    }
    return code;
}

// 添加或替换Class
function ChangeClass(element, oldClass, newClass) {

    var classArr = element.className.split(" ");
    var newClassArr = new Array();
    for (var i = 0; i < classArr.length; i++) {
        if (classArr[i] !== oldClass && classArr[i] !== newClass) {
            newClassArr.push(classArr[i]);
        }
    }

    newClassArr.push(newClass);

    element.className = newClassArr.join(" ");
}
    

